home *** CD-ROM | disk | FTP | other *** search
- // Bolo code (C) Stuart Cheshire <cheshire@cs.stanford.edu> 1987-1995.
- // All rights reserved. This code is owned by Stuart Cheshire and is donated
- // free of charge for non-commercial use. You may not use this code in any
- // product sold for commercial profit, except shareware priced at $25 or less.
-
- #include <Fonts.h> // Needed for InitFonts
- #include <TextEdit.h> // Needed for TEToScrap etc.
- #include <ToolUtils.h> // Needed for GetCursor
- #include <GestaltEqu.h> // Needed for Gestalt
- #include <Folders.h> // Needed for FindFolder
- #include <Resources.h> // Needed for GetResource
- #include <Icons.h> // Needed for GetIconSuite
- #include <Scrap.h> // Needed for ZeroScrap
- #include <LoMem.h> // Needed for CurrentA5
- #include <Traps.h> // Needed for CurrentA5
-
- #include "BrainFrame.h"
- #include "BF_ResourceIDs.h"
- #include "BF_Main.h"
- #include "BF_Globals.h"
- #include "BF_Utils.h"
- #include "BF_Prefs.h"
- #include "BF_Events.h"
- #include "BF_AppleEvents.h"
- #include "BF_IAC.h"
- #include "BF_Interface.h"
-
- local Cursor waitingcursor;
-
- // System environmental variables
- export Handle MyIconSuite;
- export short screenwidth, screenheight;
- export SysEnvRec sysenvirons;
- export short PrefsFolderWD;
- export Boolean swapMMUdefined = FALSE;
- export Boolean hasDTManager = FALSE;
- export Boolean hasGDManager = FALSE;
-
- export Boolean hasGestalt = FALSE;
- export Boolean has32bitQD = FALSE;
- export Boolean hasGWorlds = FALSE;
- export Boolean hasPowerManager = FALSE;
- export Boolean hasStdNBP = FALSE;
- export Boolean isUNIX = FALSE;
- export Boolean is32bitAddressing = FALSE;
- export Boolean hasVirtualMemory = FALSE;
- export Boolean hasExtendedTimeMgr= FALSE;
- export Boolean hasCustomFile = FALSE;
- export Boolean hasFSSpecCalls = FALSE;
- export Boolean hasAppleEvents = FALSE;
- export Boolean hasPPCToolBox = FALSE;
-
- // Progam state variables
- export Boolean frontapp = TRUE; // Are we the front application?
- export Boolean notification_posted = FALSE;
- export Boolean quitting = FALSE;
-
- local void MacInits(void)
- {
- EventRecord e;
- CursHandle curshandle;
- MaxApplZone();
- InitGraf(&qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit(); // Initialize Textedit
- TEFromScrap(); // And immediately read in the current clipboard
- InitDialogs(0);
- FlushEvents(everyEvent,0);
- EventAvail(everyEvent, &e); // Tell MF we want to be at the front!
- curshandle = GetCursor(watchCursor);
- if (curshandle && *curshandle) waitingcursor = **curshandle;
- }
-
- #ifndef _QDxDispatch
- #define _QDxDispatch 0xAB1D
- #endif
-
- local OSErr Init_EnvironmentInfo(void)
- {
- SysEnvirons(1, &sysenvirons);
- if (sysenvirons.systemVersion < 0x603)
- { InitCursor(); StopAlert(alert_systemtoostupid, NULL); return(1); }
- screenwidth = qd.screenBits.bounds.right - qd.screenBits.bounds.left;
- screenheight = qd.screenBits.bounds.bottom - qd.screenBits.bounds.top - 20;
- swapMMUdefined = TrapAvailable(_SwapMMUMode);
- hasDTManager = TrapAvailable(_DTInstall);
- hasGDManager = TrapAvailable(_QDxDispatch);
- hasGestalt = TrapAvailable(_Gestalt);
- if (hasGestalt)
- {
- long feature;
- if (Gestalt(gestaltQuickdrawVersion, &feature) == noErr)
- if (feature >= gestalt32BitQD) has32bitQD = TRUE;
- if (Gestalt(gestaltQuickdrawFeatures, &feature) == noErr)
- if (feature & (1<<gestaltHasDeepGWorlds)) hasGWorlds = TRUE;
- if (Gestalt(gestaltPowerMgrAttr, &feature) == noErr)
- if (feature & 1) hasPowerManager = TRUE;
- if (Gestalt(gestaltStdNBPAttr, &feature) == noErr)
- if (feature & (1<<gestaltStdNBPPresent)) hasStdNBP = TRUE;
- if (Gestalt(gestaltAUXVersion, &feature) == noErr) isUNIX = TRUE;
- if (Gestalt(gestaltAddressingModeAttr, &feature) == noErr)
- if (feature & (1<<gestalt32BitAddressing)) is32bitAddressing = TRUE;
- if (Gestalt(gestaltVMAttr, &feature) == noErr)
- if (feature & (1<<gestaltVMPresent)) hasVirtualMemory = TRUE;
- if (Gestalt(gestaltTimeMgrVersion, &feature) == noErr)
- if (feature >= gestaltExtendedTimeMgr) hasExtendedTimeMgr = TRUE;
- if (Gestalt(gestaltStandardFileAttr, &feature) == noErr)
- if (feature & (1<<gestaltStandardFile58)) hasCustomFile = TRUE;
- if (Gestalt(gestaltFSAttr, &feature) == noErr)
- if (feature & (1<<gestaltHasFSSpecCalls)) hasFSSpecCalls = TRUE;
- if (Gestalt(gestaltAppleEventsAttr, &feature) == noErr)
- if (feature & (1<<gestaltAppleEventsPresent)) hasAppleEvents = TRUE;
- if (Gestalt(gestaltPPCToolboxAttr, &feature) == noErr)
- {
- if (!(feature & gestaltPPCSupportsRealTime))
- { PPCInit(); Gestalt(gestaltPPCToolboxAttr, &feature); }
- if (Gestalt(gestaltPPCToolboxAttr, &feature) == noErr) hasPPCToolBox = TRUE;
- }
- }
- return(noErr);
- }
-
- local void Load_Menus(void)
- {
- SetMenuBar(GetNewMBar(128));
- AddResMenu(GetMHandle(appleID),'DRVR');
- AppleMenu = GetMHandle(appleID);
- FileMenu = GetMHandle(fileID);
- EditMenu = GetMHandle(editID);
- BFMenu = GetMHandle(BF_ID);
- DrawMenuBar();
- }
-
- local void Init_Menus(void)
- {
- CheckItem(BFMenu, bm_60, prefs.frametime == 1);
- CheckItem(BFMenu, bm_30, prefs.frametime == 2);
- CheckItem(BFMenu, bm_20, prefs.frametime == 3);
- CheckItem(BFMenu, bm_15, prefs.frametime == 4);
- }
-
- local short GetPreferencesFolder(void)
- {
- long feature;
- if (!hasGestalt || Gestalt(gestaltFindFolderAttr, &feature) != noErr)
- return(sysenvirons.sysVRefNum);
- else
- {
- short wdRef, vRefNum = 0;
- long dirID = 0;
- if (FindFolder(kOnSystemDisk, kPreferencesFolderType, kDontCreateFolder,
- &vRefNum, &dirID) != noErr) return(0);
- if (OpenWD(vRefNum, dirID, CREATOR_CODE, &wdRef) != noErr) return(0);
- //fatal("Prefs folder vrefnum %X, dirid %lX, wdref %X", vRefNum, dirID, wdRef);
- return(wdRef);
- }
- }
-
- local OSErr BF_Inits(void)
- {
- if (Init_EnvironmentInfo()) return(1);
-
- Load_Menus(); // must do this before we do getlaunchfiles
- // must do this before we read the prefs
- AppleEventInits();
- PrefsFolderWD = GetPreferencesFolder();
- readprefs(); // Don't do this until after Init_EnvironmentInfo();
- Init_Menus(); // Can't use CheckItem until prefs have been read
-
- if (!hasPPCToolBox || !sysenvirons.hasColorQD)
- { StopAlert(alert_systemtoostupid, NULL); return(1); }
- if (FreeMem() < MEMORY_REQUIREMENT) return(1);
-
- if (sysenvirons.systemVersion < 0x700) MyIconSuite = GetResource('SICN', 128);
- else GetIconSuite(&MyIconSuite, 128, 0xFFFFFFFF);
- if (!MyIconSuite) return(1);
-
- init_globals();
-
- if (PPCToolBoxInit()) return(1);
- BFInterfaceInit();
- InitCursor(); // OK, set cursor to arrow now
- return(noErr);
- }
-
- local void Run_BF(void)
- {
- openwindow();
- while(!quitting) handle_next_event(NULL,NULL);
- save_prefs(); // make sure any changed prefs are written back
- }
-
- local ProcPtr oldExitToShell;
-
- local void BF_Quit(void)
- {
- ConnectionInfo *conn = Connections;
- SetCursor(&waitingcursor); // Quitting now
- NSetTrapAddress(oldExitToShell, _ExitToShell, ToolTrap);
- while (conn) // Close all the windows and PPC connections, if any
- {
- PPCToolBoxDisconnect(conn);
- BFInterfaceClose(conn);
- conn = conn->next;
- }
- PPCToolBoxQuit(); // Close PPC port
- BFInterfaceQuit();
- SetCursor(&qd.arrow); // Finished quitting
- }
-
- local long MyExitToShell(void)
- {
- asm {
- move.l #0x12345678, a5 ; Set A5. MUST come first !
- bsr BF_Quit
- move.l oldExitToShell, a0
- jmp (a0)
- }
- }
-
- export void main(void)
- {
- MacInits();
- asm { lea MyExitToShell, a0 ; pointer to jump table entry
- move.l 2(a0), a0 ; real address of routine
- move.l a5, 2(a0) ; store A5 there
- }
- oldExitToShell = (void*)NGetTrapAddress(_ExitToShell, ToolTrap);
- NSetTrapAddress(MyExitToShell, _ExitToShell, ToolTrap);
- if (BF_Inits() == noErr) Run_BF();
- ZeroScrap(); // Just for completeness,
- TEToScrap(); // Write TEScrap back to the clipboard before exiting
- BF_Quit();
- }
-